package views.mail;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import models.mail.Mail;
import models.mail.MailParserException;
import tools.DateFormateurFR;

/**
 *
 *
 */
public class LigneMailListe extends JLabel implements ListCellRenderer
{

	private static final long serialVersionUID = 1L;
	private ImageIcon iconeNonRepondu;
	private ImageIcon iconeReponduOui;
	private ImageIcon iconeReponduNon;
	private ImageIcon iconeVide;

	/**
	 * Constructeur du Label des mails
	 */
	public LigneMailListe()
	{
		super();


		iconeNonRepondu = new ImageIcon(getClass().getResource("point-interrogation.png"));
		iconeReponduOui = new ImageIcon(getClass().getResource("valider.png"));
		iconeReponduNon = new ImageIcon(getClass().getResource("erreur.gif"));
		iconeVide = new ImageIcon(getClass().getResource("vide.png"));
	}

	/**
	 *
	 * Retourne le composant contenant la liste des mail
	 *
	 * @param list
	 * @param arg1
	 * @param arg2
	 * @param isSelected
	 * @param cellHasFocus
	 */
	public Component getListCellRendererComponent(JList list, Object arg1,
			int arg2, boolean isSelected, boolean cellHasFocus)
	{
		Mail emailAssoc = (Mail)arg1;

		if (isSelected)
			setBackground(new Color(0.7f,0.7f,1.0f));
		else
		     setBackground(Color.WHITE);

		try
		{
			String chaine = DateFormateurFR.getDateFrancaise(emailAssoc.getDateMessage());
			setText(chaine +" : "+ emailAssoc.getTitre());
		}
		catch (MailParserException e) {
			e.printStackTrace();
		}

		if(!emailAssoc.getEstLu())
			setFont(list.getFont().deriveFont(Font.BOLD));
		else
			setFont(list.getFont());

		if(emailAssoc.attendUneReponse())
		{
			if(!emailAssoc.estRepondu())
				setIcon(iconeNonRepondu);
			else if(emailAssoc.getReponse().getChoixMail() == true)
				setIcon(iconeReponduOui);
			else
				setIcon(iconeReponduNon);
		}
		else
			setIcon(iconeVide);

		setEnabled(list.isEnabled());

	      setOpaque(true);

		return this;
	}

}
